package com.example.controller;

import com.example.entity.Menu;
import com.example.entity.Subject;
import com.example.service.CourseService;
import com.example.service.JSMindService;
import com.example.service.MenuService;
import com.example.service.TeacherService;
import com.example.utils.PageUtil;
import com.example.utils.RestResponses;
import com.example.utils.VideoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/back/book")
public class BookController {
    @Resource
    TeacherService teacherService;
    @Resource
    JSMindService jsMindService;
    @Resource
    MenuService menuService;
    @Resource
    CourseService courseService;

    @GetMapping("/edit")
    public String EditBook(Model model,
                           @RequestParam(value = "book_name") String book_name,
                           @RequestParam(value = "menu_topic") String menu_topic) throws IOException {

        model.addAttribute("menu_topic", menu_topic);
        model.addAttribute("book_name", book_name);


        List<Subject> list = menuService.queryByChapterNo(menu_topic, book_name);
        model.addAttribute("list", list);

        //获取video的地址
        String video = courseService.videoUrl(book_name, menu_topic);
        if (null != video) {
            model.addAttribute("videoUrl", video);
            long duration = VideoUtil.getDuration(System.getProperty("user.dir") + "/static" + video);
            int s = courseService.videoDuration(book_name, menu_topic, duration);
            log.info("修改视频的总时长情况：" + s);
        } else
            model.addAttribute("videoUrl", "#");

        //将文件的地址添加到列表中
        String ppt = courseService.fileUrl(book_name, menu_topic);
        if (null != ppt) {
            model.addAttribute("fileUrl", ppt);
        } else {
            model.addAttribute("fileUrl", "本章尚未上传文件");
        }

        return "back/BookManage/editBook";
    }

    @ResponseBody
    @GetMapping("/getAllBooks")
    public RestResponses GetAllBooks(Integer page, Integer limit) {

        List<HashMap> hashMapList = new ArrayList<>();
        for (String book_name : teacherService.getBooks()) {
            HashMap hashMap = new HashMap<>();
            hashMap.put("book_name", book_name);
            hashMapList.add(hashMap);
        }
        HashMap hashMap = PageUtil.PageByList(hashMapList, page, limit, hashMapList.size());

        return RestResponses.ok(hashMap);
    }

    //    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    @GetMapping("/deleteBookAbout")
    public RestResponses DeleteBookAbout(String book_name) {
//        Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint();
        if (!teacherService.DeleteBookAbout(book_name)) {
//            TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint);
            return RestResponses.fail();
        }
        return RestResponses.ok();
    }

    @GetMapping("/bookManage")
    public String bookManager(@RequestParam(value = "book_name") String book_name,
                              Model model) {
        model.addAttribute("book_name", book_name);
        return "back/BookManage/BookManage";
    }

    @ResponseBody
    @GetMapping("/getBookMenu")
    public RestResponses getBookMenu(@RequestParam(value = "book_name") String book_name,
                                     Integer page, Integer limit) {
        List<HashMap<String, Object>> hashMapList = new ArrayList<>();
        List<Menu> menuList = jsMindService.BookMenu(book_name);
        for (Menu menu : menuList) {
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("topic", menu.getId() + ":" + menu.getTopic());
            hashMap.put("hasContent", menu.getHas_content());
            hashMapList.add(hashMap);
        }
        HashMap hashMap = PageUtil.PageByList(hashMapList, page, limit, hashMapList.size());
        return RestResponses.ok(hashMap);
    }


    @GetMapping("/addBookContent")
    public String addBookContent(@RequestParam(value = "book_name") String book_name,
                                 @RequestParam(value = "menu_topic") String menu_topic,
                                 Model model) {
        model.addAttribute("book_name", book_name);
        model.addAttribute("menu_topic", menu_topic);
        return "back/study/content";
    }


}
